<?php

namespace App\Http\Resources;

use App\Models\Book;
use App\Http\Resources\Book as BookResource;
use App\Http\Responses\Resource;

class Recommend extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {

        $recommendable = $this->recommendable;
        return [
            'id' => $this->id,
            'recommendable_id' => $this->recommendable_id,
            'recommendable_type' => snake_case(class_basename($this->recommendable_type)),
            'is_stress' => $this->is_stress,
            'cover_url' => $this->cover_url,
            'description' => $this->description,
            'created_at' => $this->created_at->toDateTimeString(),
            'book' => $this->when($recommendable instanceof Book,new BookResource($recommendable))

        ];

    }
}
